require("./../../js/jquery-nestable2/jquery.nestable.min.js");
import "./../../js/jquery-nestable2/jquery.nestable.min.css";
import { requestPost } from "./../../js/base/utils";

import $ from 'jquery';
global.$ = global.jQuery = $;

(function ($) {
    $('#menu-tree').nestable({
        maxDepth: 3,
        'onDragStart': function (l, e) {
            return false;
        }
    });
    $('.dd').nestable('collapseAll');

    $('.menu-tree-tools').on('click', function (e) {
        var action = $(this).data('action');
        if (action === 'expand') {
            $('.dd').nestable('expandAll');
        }
        if (action === 'collapse') {
            $('.dd').nestable('collapseAll');
        }
    });

    $("#menuSelectAll").click(function () {
        var flag = this.checked;
        $("input[type='checkbox']").each(function () {
            // 将获取的状态赋值给当前对象，
            this.checked = flag;
        });
    });

    //单选
    $("input[type='checkbox']", "#menu-tree").each(function () {
        $(this).click(function () {
            var flag = this.checked;
            //父节点取消全部取消
            $("input[type='checkbox']", $(this).parents(".dd-item:first")).each(function () {
                // 将获取的状态赋值给当前对象，
                if (!flag) this.checked = flag;
            });

            //子节点选中，父节点选中
            $("input[type='checkbox']", $(this).parents(".dd-item:first").parents(".dd-item:first").find(".dd-handle:first")).each(function () {
                // 将获取的状态赋值给当前对象，
                if (flag) this.checked = flag;
            });

        });
    })

    //保存
    $("#menuBindSave").click(function () {
        var checkIdStr = "";
        $("input[type='checkbox']", "#menu-tree").each(function () {
            if (this.checked) {
                checkIdStr += $(this).val() + ",";
            }
        });
        checkIdStr = checkIdStr.substring(0, checkIdStr.lastIndexOf(','));
        var data = { data: checkIdStr };
        requestPost(admin_api_role_bindmenu, data);
    });
})(jQuery);
